package com.controller;

import com.entity.Tops;
import com.entity.Types;
import com.service.GoodService;
import com.service.TypeService;
import com.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuronghua
 * @version 1.0
 * @data 2020-09-2020/9/23 18:34
 * @desc 编写前端控制器
 */

/**
 * 前台相关接口
 */
@Controller
@RequestMapping("/index")
public class IndexController {
    @Autowired
    private GoodService goodService;
    @Autowired
    private TypeService typeService;

    @GetMapping("/index")
    public String indextoday(HttpServletRequest request,
                             @RequestParam(required = false,defaultValue = "1") int page,
                             @RequestParam(required = false,defaultValue = "10") int size){
        request.setAttribute("flag",1);
        //今日推荐
        request.setAttribute("todayList",goodService.getListByTopType(Tops.TYPE_TODAY,page,size));
        //热销排行
        request.setAttribute("hotList",goodService.getListOrderNew(page,size));

        //类目列表
        List<Types> typeList=typeService.getList();
        List<Map<String,Object>> dataList=new ArrayList<>();
        for (Types types : typeList) {
            Map<String,Object> map =new HashMap<>();
            map.put("type",types);
            map.put("goodList",goodService.getListByType(types.getId(),page,size));
            dataList.add(map);
        }
        request.setAttribute("dataList",dataList);
        return "/index/index.jsp";
    }



    @GetMapping("/type")
    public String type(HttpServletRequest request,
                       @RequestParam(required = false,defaultValue = "0") int id,
                       @RequestParam(required = false,defaultValue = "1") int page,
                       @RequestParam(required = false,defaultValue = "10") int size
                       ){
            request.setAttribute("type",typeService.get(id));
            request.setAttribute("goodList",goodService.getListByType(id,page,size));
            request.setAttribute("pageHtml", PageUtil.getPageHtml(request,goodService.getCountByType(id),page,size));


            return "/index/goods.jsp";
    }
    @GetMapping("/today")
    public String today(HttpServletRequest request,
                        @RequestParam(required = false,defaultValue = "1") int page,
                        @RequestParam(required = false,defaultValue = "10") int size){
        request.setAttribute("flag",2);
        request.setAttribute("goodList",goodService.getListByTopType(Tops.TYPE_TODAY,page,size));
        request.setAttribute("pageHtml",PageUtil.getPageHtml(request, goodService.getCountByTopType(Tops.TYPE_TODAY),page,size));
        return "/index/goods.jsp";
    }

    @GetMapping("/hot")
    public String tohot(HttpServletRequest request,
                        @RequestParam(required = false,defaultValue = "1") int page,
                        @RequestParam(required = false,defaultValue = "10") int size){
        request.setAttribute("flag",3);
        request.setAttribute("goodList",goodService.getListOrderSales(page,size));
        request.setAttribute("pageHtml",PageUtil.getPageHtml(request, goodService.getCount(),page,size));

        return "/index/goods.jsp";
    }

    @GetMapping("/new")
    public String tonew(HttpServletRequest request,
                        @RequestParam(required = false,defaultValue = "1") int page,
                        @RequestParam(required = false,defaultValue = "10") int size){
        request.setAttribute("flag",4);
        request.setAttribute("goodList",goodService.getListOrderNew(page,size));
        request.setAttribute("pageHtml",PageUtil.getPageHtml(request, goodService.getCount(),page,size));

        return "/index/goods.jsp";
    }

    /**
     * 查询单个商品的详情
     * @param id
     * @param request
     * @return
     */

    @GetMapping("/detail")
    public String toid(int id,HttpServletRequest request){
        request.setAttribute("good",goodService.getid(id));
        request.setAttribute("today",goodService.getListByTopType(Tops.TYPE_TODAY,1,2));
        return "/index/detail.jsp";

    }


}
